﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;


namespace CCPSIP
{
    public class PCKit
    {
        /// <summary>取得Oracle連線字串(ISENDER_ORACLE)</summary>
        private static string GetConnStrIsenderOracle()
        {
            return "SERVER=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.52.195)(PORT=1521)))(CONNECT_DATA =(SERVER=DEDICATED)(SID=UNIDB)));uid=ISENDER;pwd=aKhJJFA2sWfEsQz0Zm5iwQi9gjiVND";
        }

        /// <summary>將使用者加入PC_ONLINE表格中</summary>
        /// <returns>加入成功或失敗(Boolean)</returns>
        public static bool InsertLoginData(string SipNum, string Cid)
        {
            OracleConnection connOracle = new OracleConnection(GetConnStrIsenderOracle());

            string selStrOra = "INSERT INTO PC_ONLINE(SIP_NUM, CID, LAST_LOGIN_TIME)"
                + " VALUES (:SIP_NUM, :CID, :DateTime)";

            OracleCommand selCmdOra = new OracleCommand(selStrOra, connOracle);
            selCmdOra.Parameters.Add("SIP_NUM", OracleType.VarChar).Value = SipNum;
            selCmdOra.Parameters.Add("CID", OracleType.VarChar).Value = Cid;
            selCmdOra.Parameters.Add("DateTime", OracleType.DateTime).Value = DateTime.Now;

            connOracle.Open();
            selCmdOra.ExecuteNonQuery();
            //string Beacon_UUID = Convert.ToString(selCmdOra.ExecuteScalar());
            connOracle.Close();

            return true;
        }

        /// <summary>檢查使用者是否已在PC_ONLINE表格中</summary>
        /// <returns>使用者是否已在表格中(Boolean)</returns>
        public static bool IsLogedin(string SipNum)
        {
            OracleConnection connOracle = new OracleConnection(GetConnStrIsenderOracle());

            string selStrOra = "SELECT SIP_NUM FROM PC_ONLINE"
                + " WHERE SIP_NUM = :SIP_NUM";

            OracleCommand selCmdOra = new OracleCommand(selStrOra, connOracle);
            selCmdOra.Parameters.Add("SIP_NUM", OracleType.VarChar).Value = SipNum;

            connOracle.Open();
            bool HasData = !String.IsNullOrEmpty(Convert.ToString(selCmdOra.ExecuteScalar()));
            connOracle.Close();

            return HasData;
        }
    }

    public class LoginStatus
    {
        public string SipNum { get; set; }
        public string LoginTime { get; set; }

        public LoginStatus(string sip_num, string login_time)
        {
            SipNum = sip_num;
            LoginTime = login_time;
        }
    }

    public class LogoutStatus
    {
        public string SipNum { get; set; }
        public string LogoutTime { get; set; }

        public LogoutStatus(string sip_num, string logout_time)
        {
            SipNum = sip_num;
            LogoutTime = logout_time;
        }
    }
}